package com.sonic.mquotes.report.core;

import java.util.Enumeration;

import javax.jms.JMSException;
import javax.jms.MapMessage;

import com.sonic.log.ejb.impl.MQLogger;

/** 
 * MReport processor base message consumer class.<br>
 * 
 * @author <b>Adam Dec</b> for <b><I>iBroker</i></b><br> Copyright &#169; 2008/2009
 * @since 23-03-2009
 * @category BROKER PROCESSOR MDB
 * @version 1.0
 */
public class MReportMessageProcessorBase {
	public static MQLogger logger = new MQLogger(MReportMessageProcessorBase.class, "MQ-REPORT-TOPIC-BASE");
	
	protected static final String ClientConnectedEventName 		= "ClientConnectedEvent";
	protected static final String ClientDisconnectedEventName 	= "ClientDisconnectedEvent";
	protected static final String ClientTimeoutedEventName 		= "ClientTimeoutedEvent";
	protected static final String GatewayStartedEventName 		= "GatewayStartedEvent";
	protected static final String GatewayStoppedEventName 		= "GatewayStoppedEvent";
	
	/**
	 *  Keys for all events.
	 */
    protected static final String KEY_EVENT_NAME     = "EventName";
    protected static final String KEY_CREATED_TIME   = "CreatedTime";
    protected static final String KEY_GATEWAY_NAME   = "GatewayName";
    
    /**
     * Key for client event
     */
    protected static final String KEY_SESSION_ID     = "SessionId";
    
    /**
     * Keys for client connected events
     */
    protected static final String KEY_NETWORK_ID     = "NetworkId";
    protected static final String KEY_CLIENT_TYPE    = "ClientType";
    protected static final String KEY_CLIENT_OS      = "ClientOs";
    protected static final String KEY_CLIENT_JDK     = "ClientJdk";
    protected static final String KEY_DO_STORE_FWD   = "DoStoreFwd";
    protected static final String KEY_DO_RELIABILITY = "DoReliability";
    protected static final String KEY_DO_SECURITY    = "DoSecurity";
	
    /**
     * Prefix for application connect parameters
     */
    protected static String KEY_PREFIX_APP_INFO = "X_AppInfo-";
	
    /**
     * Key for gateway started event
     */
    protected static String KEY_GATEWAY_ADDRESS = "GatewayAddress";
    protected static String KEY_TCP_PORT        = "Port.TcpClientSessionAcceptor";
    protected static String KEY_UDP_PORT        = "Port.UdpClientSessionAcceptor";
    protected static String KEY_HTTP_PORT       = "Port.HttpClientSessionAcceptor";
	
	public MReportMessageProcessorBase() { }
	
	protected void handleConnectedEvent(MapMessage connectMessage) throws JMSException {
		final String methodName = "handleConnectedEvent";
		logger.debug(methodName, " --- SessionID="  + connectMessage.getString(KEY_SESSION_ID) );
		logger.debug(methodName, " --- NetworkID="  + connectMessage.getString(KEY_NETWORK_ID) );
		logger.debug(methodName, " --- GatewayName="+ connectMessage.getString(KEY_GATEWAY_NAME) );
		logger.debug(methodName, " --- ClientType=" + connectMessage.getString(KEY_CLIENT_TYPE) );
		logger.debug(methodName, " --- ClientOS="   + connectMessage.getString(KEY_CLIENT_OS)	);
		logger.debug(methodName, " --- ClientJDK="  + connectMessage.getString(KEY_CLIENT_JDK) );			
		logger.debug(methodName, " --- DoReliability=" + connectMessage.getBoolean(KEY_DO_RELIABILITY) );
		logger.debug(methodName, " --- DoStoreFwd="    + connectMessage.getBoolean(KEY_DO_STORE_FWD) );	
		logger.debug(methodName, " --- DoSecurity="    + connectMessage.getBoolean(KEY_DO_SECURITY) );		
        
		// Look for QOS Application Parameters
		for (Enumeration<String> enumeration = connectMessage.getMapNames(); enumeration.hasMoreElements(); ) {
			String name   = (String)enumeration.nextElement();
			if ( name.startsWith(KEY_PREFIX_APP_INFO) ) {
				final String value = (String)connectMessage.getString(name);
				logger.debug(methodName, " --- QOS Application Parameter: " + name + "=" + value );
			}
		}
		
		final String sessionId = connectMessage.getString(KEY_SESSION_ID);

		//TODO
		//Add to db user's entry:
		
		// MQSessionsTable
		// ID | ClientID | SessionID | EventCreationDate
		
		// MQClientsTable
		// ID | ClientType | ClientOs | ClientJdk | DoReliability | DoStoreFwd | DoSecurity | NetworkId
		
		//TODO
		//Add to cache user's entry: sessionId
    }
    
	protected void handleDisconnectedEvent(MapMessage disconnectMessage) throws JMSException {
		final String methodName = "handleConnectedEvent";
    	String sessionId = disconnectMessage.getString(KEY_SESSION_ID);
    	
    	//get map message from cache using sessionId
    	/*
    	MapMessage connectMessage = (MapMessage)activeSessions_.get(sessionId);
    	if ( connectMessage == null ) {
    		logger.debug(methodName, " --- handleDisconnectedEvent:WARNING No session registred!"); 
    		return;
    	}
    	long disconnectTime = disconnectMessage.getLong(KEY_CREATED_TIME);
    	long connectTime    = connectMessage.getLong(KEY_CREATED_TIME);
    	logger.debug(methodName, " --- SessionID="  + connectMessage.getString(KEY_SESSION_ID) );
    	logger.debug(methodName, " --- Session-duration="  + (disconnectTime - connectTime)/1000 + "[sec]" );
    	*/
    	
    	//remove sessionId from cache    	
    }

	protected void handleGatewayStartedEvent(MapMessage gatewayMessage) throws JMSException {
		final String methodName = "handleConnectedEvent";
		logger.debug(methodName, " --- GatewayAddress="+ gatewayMessage.getString(KEY_GATEWAY_ADDRESS) );

		for (Enumeration<String> enumeration = gatewayMessage.getMapNames(); enumeration.hasMoreElements(); ) {
			final String name = (String)enumeration.nextElement();
			if ( name.startsWith("Port.") ) {
				logger.debug(methodName, " --- " + name + "="+ gatewayMessage.getString(name) );
			}
		}
	}
}